import os
import glob
import sys
import dicom
def process(dir="./"):
    try:
        os.chdir(dir)
        print os.getcwd()
        file_list = glob.glob("*")
        for file in file_list:
	    try:
                dfile = dicom.dicom(filename=file)
                hdr = dfile.dicomInfo()
                print file, hdr['InstanceNumber'],type(hdr['InstanceNumber'])
                newname = "I%05d.dcm"%hdr['InstanceNumber']
	        #os.system("cp %s %s" %(file,newname))

                os.rename(file , newname)
	    except Exception, error:
	        print error
    except Exception, error:
        print "failed in rename_dicoms.process(): ", error

def main():
    print sys.argv
    process(dir=sys.argv[1])
	
if __name__=='__main__':
    main()


